package com.cupk.service.impl;

import com.cupk.controller.StageController;
import com.cupk.mapper.MatchMapper;
import com.cupk.mapper.RecordMapper;
import com.cupk.mapper.StageMapper;
import com.cupk.pojo.Match;
import com.cupk.pojo.Result;
import com.cupk.pojo.Stage;
import com.cupk.service.StageService;
import com.cupk.utils.ThreadLocalUtil;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.imgproc.Imgproc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StageServiceImpl implements StageService {
    @Autowired
    private StageMapper stageMapper;
    @Autowired
    private MatchMapper matchMapper;

    @Override
    public Boolean judgeLegal(Stage stage) {
        Integer matchId = stage.getMatchId();
        Match m = matchMapper.getMatchById(matchId);
        if (m == null) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean judgeLegal(Integer matchId, Integer userId, Integer role) {
        Match m = matchMapper.getMatchById(matchId);
        if (m == null) {
            return false;
        }
        if (userId != m.getUser() && role != 0) {
            return false;
        }
        return true;
    }

    @Override
    public void addStage(Stage stage) {
        stageMapper.addStage(stage);
    }

    @Override
    public Integer findMaxPeriod(Integer matchId) {
        Integer res = stageMapper.findMaxPeriod(matchId);
        System.out.println(res);
        if (res == null) {
            res = 1;
        }
        return res;
    }

    @Override
    public List<Stage> getListByMatchId(Integer matchId) {
        return stageMapper.getStageListByMatch(matchId);
    }

    @Override
    public Stage getStageById(Integer stageId) {
        return stageMapper.getStageById(stageId);
    }

    @Override
    public void updateStage(Stage stage) {
        stageMapper.updateStage(stage);
    }

    @Override
    public void deleteStage(Integer stageId) {
        stageMapper.deleteStage(stageId);
    }

    @Override
    public List<Stage> getEffectListByMatchId(Integer matchId) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        LocalDateTime nowTime = LocalDateTime.now();
        return stageMapper.getEffectListByMatchAndStudent(matchId, userId, nowTime);
    }

    @Override
    public Integer getAcceptPeople(Integer stageId) {
        return stageMapper.getAcceptPeople(stageId);
    }

    @Override
    public Integer getUnAcceptPeople(Integer stageId) {
        return stageMapper.getUnAcceptPeople(stageId);
    }

    @Override
    public Integer findMaxStage(Integer matchId) {
        Integer res = stageMapper.findMaxStage(matchId);
        if(res == null){
            res = 0;
        }
        return res;
    }

    @Override
    public Integer findMinStage(Integer matchId) {
        Integer res = stageMapper.findMinStage(matchId);
        if(res == null){
            res = 0;
        }
        return res;
    }

}